#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import datetime
from django.utils import simplejson
import json

from model import Question

class MyHandler(webapp.RequestHandler):
    
    def get(self):
        questions = db.Query(Question)

#        self.response.out.write(simplejson.dumps(questions))
        questionsObj = []
        json = "["

        for q in questions:
            answersArr = []
            for a in q.answers:
                answersArr.append({ "text": a})
                #json += "{ text:" + str(a) + "},"
            
            obj = { "text": q.text, "answers":answersArr}
            questionsObj.append(obj)
#            json += "{ text:" + q.text + ", answers:["
            
 #           json = json[:-1]
  #          json += "]},"


   #     if len(json) > 1:
    #        json = json[:-1]

     #   json += "]"
#        self.response.out.write(simplejson.dumps(json))
        self.response.out.write(simplejson.dumps(questionsObj))
                            
def main():
    app = webapp.WSGIApplication([
        (r'.*', MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()